var scrapCode = getQueryString("scrapNo");
$(function () {
    $("#jqGrid").Grid({
        url: '../sparescrapdetail/list?scrapCode=' + scrapCode,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '报废编码', name: 'scrapCode', index: 'SCRAP_CODE', width: 80},
            {label: '物料编码', name: 'materialCode', index: 'MATERIAL_CODE', width: 80},
            {label: '物料名称', name: 'materialName', index: 'MATERIAL_NAME', width: 80},
            {label: '使用部门', name: 'useDept', index: 'USE_DEPT', width: 80},
            {label: '型号规格', name: 'sepc', index: 'SEPC', width: 80},
            {label: '供货商', name: 'supplyName', index: 'SUPPLY_NAME', width: 80},
            {label: '数量', name: 'num', index: 'NUM', width: 80},
            {label: '报废原因', name: 'scrapReson', index: 'SCRAP_RESON', width: 80},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {
                label: '操作时间', name: 'createTime', index: 'CREATE_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {label: '审核人', name: 'approvalUserName', index: 'APPROVAL_USER', width: 80},
            {
                label: '审核时间', name: 'approvalTime', index: 'APPROVAL_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '审核状态',
                name: 'approvalStatus',
                index: 'APPROVAL_STATUS',
                width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value]
                }
            },
            /*{label: '修改时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80},
            {label: '修改人', name: 'updateUser', index: 'UPDATE_USER', width: 80},
            {label: '操作人', name: 'createUser', index: 'CREATE_USER', width: 80},

            {label: '删除标识', name: 'delFlag', index: 'DEL_FLAG', width: 80},*/


        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        spareScrapDetail: {
            updateTime: '',
            updateUser: '',
            createUser: '',
            createTime: '',
            delFlag: '',
            remark: '',
            approvalStatus: '',
            approvalTime: '',
            approvalUser: '',
            scrapReson: '',
            num: '',
            supplyName: '',
            sepc: '',
            useDept: '',
            materialName: '',
            materialCode: '',
            scrapCode: '',
            id: '',
        },
        ruleValidate: {
            materialCode: [
                {required: true, message: '物料编码不能为空', trigger: 'blur'}
            ],
            num: [
                {required: true, message: '报废数量不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.spareScrapDetail = {
                updateTime: '',
                updateUser: '',
                createUser: '',
                createTime: '',
                delFlag: '1',
                remark: '',
                approvalStatus: '0',
                approvalTime: '',
                approvalUser: '',
                scrapReson: '',
                num: '',
                supplyName: '',
                sepc: '',
                useDept: '',
                materialName: '',
                materialCode: '',
                scrapCode: scrapCode,
                id: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.spareScrapDetail.id == (null || '') ? "../sparescrapdetail/save" : "../sparescrapdetail/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.spareScrapDetail),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../sparescrapdetail/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../sparescrapdetail/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.spareScrapDetail = r.spareScrapDetail;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        review: function () {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            if (ids == null) {
                return;
            }
            if (rows.approvalStatus == "已审核") {
                alert('审核已通过,请勿重复审核!', function (index) {
                    vm.reload();
                });
                return;
            }
            vm.getInfo(ids);
            openWindow({
                title: "审核",
                area: ['550px', '300px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    Ajax.request({
                        url: '../sparescrapdetail/review',
                        params: JSON.stringify(vm.spareScrapDetail),
                        type: 'POST',
                        contentType: "application/json",
                        successCallback: function (result) {
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        }
                    });
                }
            });
        },
        //显示备件列表-物料编码
        getSparePart: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择备件',
                content: '../spare/sparePartSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSparePart();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.sparePart = res;
                        vm.spareScrapDetail.materialCode = vm.sparePart.partCode;
                        vm.spareScrapDetail.materialName = vm.sparePart.name;
                        vm.spareScrapDetail.sepc = vm.sparePart.spec;
                        vm.spareScrapDetail.useDept = vm.sparePart.useUnit;
                    }
                }
            })
        },
    }
});